package com.purong.purong.fragment;

import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.purong.purong.Contant;
import com.purong.purong.R;
import com.purong.purong.activity.SmallLoanDetialActivity;
import com.purong.purong.adapter.SmallLoanAdapter;
import com.purong.purong.bean.SmallLoanBean;
import com.purong.purong.bean.SmallLoanListBean;
import com.purong.purong.util.CommonUtil;
import com.purong.purong.util.SharePreUtil;
import com.purong.purong.widget.LoadingView;
import com.yao.sdk.net.ResponseInfo;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import in.srain.cube.views.ptr.PtrCustomHandler;
import in.srain.cube.views.ptr.PtrDefaultCustomHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrPuRongLayout;

/**
 * Created by wenjiezeng on 2017/11/10.
 */
public class SmallLoanFragment extends BaseFragment {
    private PtrPuRongLayout mPtrFrame;
    private ListView mListView;
    private View headView;
    private SmallLoanAdapter mAdapter;
    private List<SmallLoanBean> loanList = new ArrayList<>();
    private LoadingView loadView;
    private ImageView bannerImg;
    private int page=1;
    @Override
    public int getRootViewId() {
        return R.layout.fragment_speed_loan;
    }

    @Override
    public void onPause() {
        super.onPause();
        new Handler().postDelayed(new Runnable() {
            public void run() {
                if (mPtrFrame!=null&&mPtrFrame.isRefreshing()) {
                    mPtrFrame.refreshComplete();
                }
            }
        }, 100);
    }

    @Override
    public void initView(View rootView) {
        loadView=(LoadingView)rootView.findViewById(R.id.loading_view);
        loadView.setNoResultToast("暂无相关贷款","");
        loadView.setLoadListener(new LoadingView.OnLoadListener() {
            @Override
            public void onReload() {
                getSmallLoan(true);
            }

            @Override
            public void onRefresh() {
                getSmallLoan(true);
            }
        });
        mPtrFrame = (PtrPuRongLayout)rootView.findViewById(R.id.rotate_header_list_view_frame);
        mPtrFrame.setPtrHandler(new PtrCustomHandler() {
            @Override
            public boolean checkCanDoLoadMore(PtrFrameLayout frame, View content, View footer) {
                if (loanList.size()==0){
                    return false;
                }else {
                    return PtrDefaultCustomHandler.checkContentCanBePulledUp(frame, content, footer);

                }
            }

            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                getSmallLoan(false);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getSmallLoan(true);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultCustomHandler.checkContentCanBePulledDown(frame, content, header);
            }
        });
        mListView=(ListView)rootView.findViewById(R.id.list_view);
        headView = getActivity().getLayoutInflater().inflate(R.layout.speed_loan_head_view, null);

        bannerImg=(ImageView) headView.findViewById(R.id.banner_img);
        bannerImg.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,(int)(CommonUtil.getScreenWidth(getActivity())/2.286)));
        mAdapter=new SmallLoanAdapter(getActivity(),loanList);
        mListView.addHeaderView(headView);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position!=0){
                    Intent intent=new Intent(getActivity(),SmallLoanDetialActivity.class);
                    intent.putExtra("LOAN",loanList.get(position-1));
                    startActivity(intent);
                }
            }
        });
    }
    private void getSmallLoan(boolean clear){
        cancelRequest();
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("city_id", SharePreUtil.getInstance(getActivity()).getLoactionAreaId());
        params.put("province_id", SharePreUtil.getInstance(getActivity()).getLoactionParentId());
        if (clear) {
            page =1;
            if (loanList.size()==0){
                loadView.loading();
            }
        }

        params.put("page", page + "");
        if (!request(Contant.SMALL_SEARCH_LIST, params, true)) {
            if (clear&&loanList.size()==0)
                loadView.fail();
            if (!hasDestroy())
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        if (mPtrFrame.isRefreshing()) {
                            mPtrFrame.refreshComplete();
                        }
                    }
                }, 100);
        }

    }

    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (hasDestroy())
            return;
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.SMALL_SEARCH_LIST)){
            if (mPtrFrame.isRefreshing()) {
                mPtrFrame.refreshComplete();
            }
            if (responseInfo.isRequestOk){
                try{
                    SmallLoanListBean bean= JSON.parseObject(responseInfo.content,SmallLoanListBean.class);
                    if (responseInfo.params.get("page").equals("1"))
                        loanList.clear();
                    if (bean!=null&&bean.getSmall_search_list()!=null){
                        if (bean.getSmall_search_list().size()>0)
                            page++;
                        loanList.addAll(bean.getSmall_search_list());

                    }
                    mAdapter.notifyDataSetChanged();
                    if (loanList.size()==0){
                        loadView.noResultWithRefresh();

                    }else {
                        loadView.finish();
                    }
                }catch (Exception e){
                    if (page == 1){
                        loadView.fail();
                    }
                }
                Log.e("zwj",responseInfo.content);
            }else {
                if (page == 1){
                    loadView.fail();
                }
            }
        }
    }

    @Override
    public void initData() {
        getSmallLoan(true);
    }

    public void scroll2Top(){
        if (mListView!=null&&loanList!=null){
            mListView.setSelection(0);
        }

    }
}
